var dictKeyArray = [];
layui.use(['element','table','form','layer','laydate'], function(){
    var table = layui.table,
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    layer = layui.layer;

    table.render({
        elem: '#deviceRecordTable'
        ,url:'/unAuth/cdys7lives/listDevice'
        ,title: '设备列表'
        ,where: {}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'deviceSerial', title:'设备序列号',align:'center'}
            ,{field:'deviceName', title:'设备名称',align:'center'}
            ,{field:'deviceType', title:'设备类型',align:'center'}
            ,{field:'deviceVersion', title:'设备版本号',align:'center'}
            ,{field: 'addTime', title: '添加时间', align:'center',templet: function (d) {
                    return formatTime(d.addTime,"yyyy-MM-dd hh:mm:ss");
                }}
            ,{field: 'status', title: '设备状态', align:'center',templet: function (d) {
                    return parseStatus(d.status);
                }}
            , {field: 'right', title: '操作', align:'center',width:200,templet:function(data){
                    return '<a class="layui-btn layui-btn-xs" lay-event="monitor1">监控(流畅)</a>' +
                        '<a class="layui-btn layui-btn-xs" lay-event="monitor2">监控(高清)</a>';
                }
            }
        ]]
        ,page: false
    });
    table.on('tool(deviceRecordTable)', function(obj){
        let data = obj.data;
        switch (obj.event){
            case 'monitor1':
                getLiveAddress(data.deviceSerial,"2");//流畅
                break;
            case 'monitor2':
                getLiveAddress(data.deviceSerial,"1");//高清
                break;
        }
    });

    function getLiveAddress(deviceSerial,quality) {
        var index = layer.load();
        $.get("/unAuth/cdys7lives/getLiveAddress", {"deviceSerial":deviceSerial,"quality":quality}, function (result) {
            layer.close(index);
            if (result.resultCode === '000') {
                var urlToken = result.resultContent;
                if(urlToken){
                    $("#url").val(urlToken.url);
                    $("#accessToken").val(urlToken.token);
                    layer.open({
                        type: 1
                        , title: '监控模式'
                        , shadeClose: true
                        , area: ['99%', '99%']
                        , content: $('#monitorWindow')
                    });
                }else {layer.msg("参数有误", {icon: 5});}
            } else {
                layer.msg(result.resultDesc, {icon: 5});
            }
        });
    }

    var playr;
    $("#init").on('click',function (){
        var token = $("#accessToken").val();
        var url = $("#url").val();
        if(!token || !url){return layer.msg("获取播放地址失败",{icon:5})}
        if(playr){return layer.msg("已经初始化",{icon:5})}
        playr = new EZUIKit.EZUIKitPlayer({
            id: 'video-container', // 视频容器ID
            accessToken: token,
            url: url,
            template: 'simple', // simple - 极简版;standard-标准版;security - 安防版(预览回放);voice-语音版；
            autoplay: true,
            // 视频上方头部控件
            //header: ['capturePicture','save','zoom'],            // 如果templete参数不为simple,该字段将被覆盖
            // 视频下方底部控件
            //footer: ['talk','broadcast','hd','fullScreen'],      // 如果template参数不为simple,该字段将被覆盖
            audio: 1, // 是否默认开启声音 0 - 关闭 1 - 开启
            plugin: ['talk'],                       // 加载插件，talk-对讲
            openSoundCallBack: (data) => console.log("开启声音回调",data),
            closeSoundCallBack: (data) => console.log("关闭声音回调",data),
            startSaveCallBack: (data) => console.log("开始录像回调",data),
            stopSaveCallBack: (data) => console.log("录像回调",data),
            capturePictureCallBack: (data) => console.log("截图成功回调",data),
            fullScreenCallBack: (data) => console.log("全屏回调",data),
            getOSDTimeCallBack: (data) => console.log("获取OSDTime回调",data),
            handleSuccess: (data) = function(){console.log("播放成功回调",data)},
            handleError: (data) => console.log("播放失败回调",data),
            width:600,
            height:400,
        });
    });
    $("#play").on('click',function (){
        if(!playr){return layer.msg("请先初始化",{icon:5})}
        playr.play();
    });
    $("#stop").on('click',function (){
        if(!playr){return layer.msg("请先初始化",{icon:5})}
        playr.stop();
    });

});

function parseStatus(status) {
    if(status){
        switch (status) {
            case 0 : return "离线";
            case 1 : return "在线";
            default: return "未知";
        }
    }
    return "未知";
}
function reload(){
    location.reload();
}
function formatTime(datetime,fmt) {
    if (parseInt(datetime)==datetime) {
        if (datetime.length==10) {
            datetime=parseInt(datetime)*1000;
        } else if(datetime.length==13) {
            datetime=parseInt(datetime);
        }
    }
    datetime=new Date(datetime);
    var o = {
        "M+" : datetime.getMonth()+1,                 //月份
        "d+" : datetime.getDate(),                    //日
        "h+" : datetime.getHours(),                   //小时
        "m+" : datetime.getMinutes(),                 //分
        "s+" : datetime.getSeconds(),                 //秒
        "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
        "S"  : datetime.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}